package com.hzw.controller;

import com.hzw.api.Api;
import com.hzw.bean.Article;
import com.hzw.dto.ApiResult;
import com.hzw.dto.ArticleCondition;
import com.hzw.dto.ArticleParam;
import com.hzw.dto.ArticleResponse;
import com.hzw.service.ArticleService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/3/14.
 */
@Controller
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    /**
     * 新增博文
     * @param articleParam
     * @return
     */
    @RequestMapping(value = "/addArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addArticle(@RequestBody ArticleParam articleParam){
        ApiResult result = new ApiResult();
        Article article = articleParam.getArticle();
        if(ObjectUtils.isNullOrEmpty(article)){
            result.error(CLIENT_ERROR,"文章不能为空！");
            return result;
        }else {
            try {
                articleService.addArticle(articleParam);
                result.setMsg("文章插入成功！");
            }catch (Exception e){
                result.error(Api.SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 通过文章Id删除文章(软删除)
     * @param article
     * @return
     */
    @RequestMapping(value = "/deleteArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteArticleById(@RequestBody Article article){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(article) || ObjectUtils.isNullOrEmpty(article.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else {
            try{
                String articleId = article.getId();
                int count = articleService.deleteArticleById(articleId);
                if(count == 0){
                    result.error(CLIENT_ERROR,"文章id有误！");
                    return result;
                }else if(count == 1){
                    result.setMsg("删除成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     *通过Id更新文章的非空字段
     * @param articleParam
     * @return
     */
    @RequestMapping(value = "/updateArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateArticleById(@RequestBody ArticleParam articleParam){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleParam) || ObjectUtils.isNullOrEmpty(articleParam.getArticle().getId())){
            result.error(CLIENT_ERROR,"缺少参数:id");
            return result;
        }else {
            try{
                int count = articleService.updateArticleById(articleParam);
                if(count == 0){
                    result.error(SERVER_ERROR,"更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("更新成功！");
                    return result;
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 通过articleId查找博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/selectArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleById(@RequestBody Article article){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(article) || ObjectUtils.isNullOrEmpty(article.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String articleId = article.getId();
                ArticleResponse articleResponse = articleService.selectArticleById(articleId);
                if (ObjectUtils.isNullOrEmpty(articleResponse.getArticle())) {
                    result.setMsg("找不到此id对应的博文！");
                }else {
                    result.setData(articleResponse);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 按条件查找博文列表，三种场景 1、大厅(所有博文) 2、个人首页（自己和好友的博文） 3、个人博文
     * @param articleCondition
     * @return
     */
    @RequestMapping(value = "/selectArticleByCondition",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleByCondition(@RequestBody ArticleCondition articleCondition){
        ApiResult result = new ApiResult();
        try{
            List<ArticleResponse> list = articleService.selectArticleByCondition(articleCondition);
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到对应的博文！");
            }else {
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 根据关键词模糊搜索相关博文
     * @param
     * @return
     */
    @RequestMapping(value = "/selectArticleByKeyWord",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleByKeyWord(@RequestBody Article article){
        ApiResult result = new ApiResult();

        try{
            List<ArticleResponse> list = articleService.selectArticleByKeyWord(article.getTitle());
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到相应的博文！");
            }else {
                result.setMsg("查找相关博文成功！");
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 查找最新的文章列表（前十条）
     * @param
     * @return
     */
    @RequestMapping(value = "/selectNewestArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectNewestArticle(){
        ApiResult result = new ApiResult();
        try{
            List<ArticleResponse> list = articleService.selectNewestArticle();
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到最新的博文！");
            }else {
                result.setMsg("查找最新文章成功！");
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }

    /**
     *客户端推荐列表：获取推荐文章列表十条
     * @return
     */
    @RequestMapping(value = "/selectRecommendArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectRecommendArticle(){
        ApiResult result = new ApiResult();
        try{
            List<ArticleResponse> articleResponseList = articleService.selectRecommendArticle();
            result.setData(articleResponseList);
        }catch (Exception e){
            return result.error(SERVER_ERROR,e.getMessage());
        }
        return result;
    }
}
